package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.entity.Setmeal;
import com.itheima.entity.SetmealDish;
import com.itheima.mapper.SetmealMapper;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.lang.model.element.VariableElement;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 新增套餐，同时需要保存套餐和菜品的关联关系
 *
 * @param
 */
@Service
@Transactional
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        this.save(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes(); //已经有菜品ID了从前端传来的，在套餐于菜品中还缺少套餐ID
        setmealDishes.stream().map((item) -> {
            Long setmealId = setmealDto.getId();
            item.setSetmealId(setmealId);
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 删除套餐，同时需要删除套餐和菜品的关联数据
     *
     * @param ids
     */

    public void removeWithDish(List<Long> ids) {
        //查询套餐状态，确定是否可用删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId, ids);
        queryWrapper.eq(Setmeal::getStatus, 1);
        int count = this.count(queryWrapper);
        if (count > 0) {
            throw new CustomException("套餐正在售卖中，不能删除");
        }
        //如果可以删除，先删除套餐表中的数据---setmeal
        this.removeByIds(ids);

        //删除关系表中的数据----setmeal_dish
        //delete from setmeal_dish where setmeal_id in (1,2,3)
        //因为仙子啊传过来的IDS是setmeal套餐里面的id所以不能直接setmealDishService用
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(lambdaQueryWrapper);

    }


    /*
    * 套餐修改
    *
    * */
    //根据id查询当前套餐信息和对应的套餐菜品，用于修改页面中套餐信息回显。
    //根据ID查询套餐的基本信息
    //根据套餐的ID查询套餐菜品列表数据
    //组装数据并返回
    public SetmealDto getByIdWithDish(Long id) {
        //查询套餐基本信息，从Setmeal表查询
        Setmeal setmeal = this.getById(id);

        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);

        //查询当前套餐对应的套餐菜品信息，从Setmeal_Dish表查询
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        //select * from setmeal_dish where setmealid = ?
        //根据当前套餐id查询对应的套餐与套餐菜品关系表中与之相对应的套餐ID，获取出关联的菜品。
        queryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        //查询出来后封装成一个集合
        List<SetmealDish> dishes = setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(dishes);
        return setmealDto;
    }


    @Transactional
    public void UpdateWithDish(SetmealDto setmealDto) {
        //更新setmeal表基本信息
        this.updateById(setmealDto);
        //根据当前的套餐ID查询套餐菜品表中与之对应的菜品，调用service的remove方法删除当前数据
        LambdaQueryWrapper<SetmealDish>queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        //添加当前提交过来的菜品数据
        //获取当前新增菜品
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //通过stream流获取每一个setmealDishes套餐菜品，根据菜品的ID存放到对应的套餐中
        List<SetmealDish> setmealDishes1 = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes1);
    }

}


